﻿using System;
using CCL.SocketMessage;
using CCL.SocketMessage.Request;
using CCL.Utility;
using Server.Network;
using System.Net.Sockets;

namespace Server
{
    public class Program
    {
        // ReadyToFileTransfer Pool

        static void Main(string[] args)
        {
            new AsyncServer().RunProc(AsyncServerRecievedEvent);
        }

        //public static byte[] AsyncServerRecievedEvent(byte[] byteArray)
        public static byte[] AsyncServerRecievedEvent(SocketAsyncEventArgs se)
        {
            byte[] headerBytes = new byte[17];
            Array.Copy(se.Buffer, headerBytes, 17);
            Header header = new Header(headerBytes);

            int lastLength = se.BytesTransferred - 17;
            byte[] bodyBytes = new byte[header.BodyLength];
            Array.Copy(se.Buffer, 17, bodyBytes, 0, lastLength);

            while (true)
            {
                if (lastLength == header.BodyLength)
                    break;

                byte[] recvBytes = new byte[1024];
                int recvLen = se.AcceptSocket.Receive(recvBytes);

                Array.Copy(bodyBytes, lastLength, recvBytes, 0, recvLen);
                lastLength += recvLen;
            }

            Service.ServiceManager serviceMgr = new Service.ServiceManager(header, bodyBytes);

            return serviceMgr.GetByte();
        }
    }
}
 